/**
 * 
 */
package sg.edu.nus.iss.vmcs.payment;

import java.awt.Button;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;

import sg.edu.nus.iss.vmcs.store.CashStore;
import sg.edu.nus.iss.vmcs.system.Fault;
import sg.edu.nus.iss.vmcs.system.FaultType;
import sg.edu.nus.iss.vmcs.util.Log;
import sg.edu.nus.iss.vmcs.util.WarningDisplay;

/**
 * @author moe
 *
 */
public class NetsUI extends Panel  {
	private NetsReader ctrl = null;
	private NetsListener listener = null;
	
	private Button btnInsertNets = null;
	private Button btnNetsProblem = null;
	Checkbox cbValidCard = null;
	Checkbox cbPaymentStatus = null;
	
	private WarningDisplay statusDisplay = null;
	private WarningDisplay cardEjected = null;
	
	public NetsUI(Panel composite, NetsReader controller) {
		this.ctrl = controller;
		listener = new NetsListener(ctrl);

		this.setLayout(new GridLayout(0, 3));

		// add title
		Label lb = new Label("Enter NETS Here", Label.LEFT);
		this.add(lb);

		Panel cbPanel = new Panel();
		cbPanel.setLayout(new GridLayout(0,2));
		cbValidCard = new Checkbox("Valid Card", true);
		cbPaymentStatus = new Checkbox("Accept Payment", true);
		cbPanel.add(cbValidCard);
		cbPanel.add(cbPaymentStatus);
		this.add(cbPanel);
		
		// add buttons
		Panel btnPanel = new Panel();
		btnPanel.setLayout(new GridLayout(0, 1));

		btnInsertNets = new Button("Insert Nets");
		btnInsertNets.setActionCommand(NetsListener.CARD_INSERTED);
		btnInsertNets.addActionListener(listener);		
		btnPanel.add(btnInsertNets);
		
		this.add(btnPanel);
		
		statusDisplay = new WarningDisplay("                    *******                       ");
		this.add(statusDisplay);
		cardEjected = new WarningDisplay("Card Ejected");
		this.add(cardEjected);
		Button btnColCard = new Button("Card Collected");
		btnColCard.setActionCommand(NetsListener.CARD_COLLECTED);
		btnColCard.addActionListener(listener);	
		btnColCard.setVisible(false);
		this.add(btnColCard);
		
		btnNetsProblem = new Button("Clear Nets Error");
		btnNetsProblem.addActionListener(listener);
		btnNetsProblem.setActionCommand(CashStore.COIN_RECEIVER_CLEAR_ERR);
		btnNetsProblem.setEnabled(true);
		setProblemButton(false);
		this.add(btnNetsProblem);
	}
	
	/**
	 * Set the Problem Button.
	 * @param err
	 * If err is 'True', it indicate as currently Coin Receiver has error
	 */
	public void setProblemButton(boolean err){
		if (err){
			btnNetsProblem.setLabel("Clear NETS Error");
			btnNetsProblem.setActionCommand(NetsListener.NO_NETS_ERROR);
		}else{
			btnNetsProblem.setLabel("Set NETS Error");
			btnNetsProblem.setActionCommand(NetsListener.NETS_ERROR);
		}
	}
	

	public void setBtnActive(boolean b) {
		btnInsertNets.setEnabled(b);
	}
	
	public void setStatusActive(boolean b){
		cbValidCard.setEnabled(b);
		cbPaymentStatus.setEnabled(b);
	}
	
	public void setActive(boolean b){
		Log.LogIt("NetsUI.setActive:" + b);
		setBtnActive(b);
		setStatusActive(b);
		setWarning("                                             ");
		showEject(false);
	}
	
	public Boolean isValidCard(){
		return cbValidCard.getState();
	}
	
	public Boolean isPaymentAccepted(){
		return cbPaymentStatus.getState();
	}
	
	public void setWarning(String msg){
		statusDisplay.setText(msg);
		if (msg.trim().isEmpty()){
			statusDisplay.setState(false);
		}else{
			statusDisplay.setState(true);
		}
	}
	
	public void showEject(boolean ejected){
		cardEjected.setState(ejected);
	}
}
